package com.bazinga.contact.view;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.bazinga.contact.model.RecommendedJob;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StudentHomeAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> session;
	private final String dbServer = "127.0.0.1";
	private List<RecommendedJob> recommendedJobList;
	private String fname = null;

	public String getFname() {
		return fname;
	}
	public void setFname(String fname) {
		this.fname = fname;
	}
	public List<RecommendedJob> getRecommendedJobList() {
		return recommendedJobList;
	}
	public void setRecommendedJobList(List<RecommendedJob> recommendedJobList) {
		this.recommendedJobList = recommendedJobList;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	/**
     * @see HttpServlet#HttpServlet()
     */
	
	public String execute() {
		this.session = ActionContext.getContext().getSession();
		if(!session.containsKey("logged-in")) {
			return "login";
		}
		
		fname = session.get("firstname").toString();
		String userid = session.get("userid").toString();
		String dbUrl = "jdbc:mysql://" + dbServer + ":3306/";
		String dbName = "bazinga";
		String driverName = "org.gjt.mm.mysql.Driver";
		String username = "root";
		String password = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		String searchQuery = "select J.jobid, J.title, J.salary, J.description, J.organisation, J.city from jobdetails J,userdetails U where J.city = U.city and U.userid=" + userid + " order by J.jobid desc;";
		try
		{
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dbUrl + dbName, username, password);
			stmt = conn.createStatement();
		}
		catch (Exception e) {
			System.out.println(e.toString());
		}
		try {
			rs = stmt.executeQuery(searchQuery);
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
			return "error";
		}

		recommendedJobList = new ArrayList<RecommendedJob>();
		try {
			while(rs.next()) {
				String title = rs.getString("title").toString();
				String org = rs.getString("organisation").toString();
				String salary = rs.getString("salary").toString();
				String city = rs.getString("city").toString();
				String description = rs.getString("description").toString();
				int jobid = Integer.parseInt(rs.getString("jobid").toString());
				
				RecommendedJob temp = new RecommendedJob();
				
				temp.setCity(city);
				temp.setSalary(salary);
				temp.setOrganisation(org);
				temp.setTitle(title);
				temp.setDescription(description);
				temp.setJobid(Integer.toString(jobid));
				if(jobid % 2 == 0)
					temp.setHotness("Hot Job!");
				
				recommendedJobList.add(temp);
			}
		}
		
		
		catch(SQLException ex) {
			ex.printStackTrace();
		}
		
		catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return "success";
	}
    public StudentHomeAction() {
        super();
        // TODO Auto-generated constructor stub
    }

}
